/*
 * Copyright 2006 - Chris Maki
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.agoracms.model;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

@Entity
@Table(name="private_message")
public class PrivateMessage extends ModelBase implements Serializable {

    private static final long serialVersionUID = -2016234883029898658L;
    
    @Lob
    @Column(name="message")
    protected String message; 
    
    @Temporal(value=TemporalType.TIMESTAMP)
    @Column(name="date_read", nullable=true)
    protected Date dateRead;
    
    @Temporal(value=TemporalType.TIMESTAMP)
    @Column(name="date_replied", nullable=true)
    protected Date dateReplied;
    
    @Column(name="number")
    protected String number;
    
    @ManyToOne(cascade=CascadeType.PERSIST)
    private User toUser;
    
    @ManyToOne(cascade=CascadeType.PERSIST)
    private User fromUser;

    @Transient
    protected boolean responseSent;
    @Column(name="subject")
    protected String subject;
    
    public Date getDateRead() {
        return dateRead;
    }
    public void setDateRead(Date dateRead) {
        this.dateRead = dateRead;
    }

    public String getMessage() {
        return message;
    }
    public void setMessage(String message) {
        this.message = message;
    }
    public String getSubject() {
        return subject;
    }
    public void setSubject(String subject) {
        this.subject = subject;
    }
    public boolean isResponseSent() {
        if (dateReplied != null)
            responseSent = true;
        return responseSent;
    }
    public Date getDateReplied() {
        return dateReplied;
    }
    public void setDateReplied(Date dateReplied) {
        this.dateReplied = dateReplied;
    }
    public User getToUser() {
        return toUser;
    }
    /**
	 * This method assumes that the client will have both the toUser and the
	 * fromUser and attach them to this PM using the setToFromUser
	 * 
	 * @param toUser
	 *            the user receiving this message
	 * @see #setToFromUser(User, User)
	 */
    public void setToUser(User toUser) {
        this.toUser = toUser;
		// manage both sides of the relationship
        toUser.addMessage(this);
    }
    
    public User getFromUser() {
		return fromUser;
	}
    /**
	 * This sets the user that sent the pm, you will need to also need to invoke
	 * setToUser or just use setToFromUser.
	 * 
	 * @param fromUser the user sending this PM
	 * @see #setToFromUser(User, User)
	 */
	public void setFromUser(User fromUser) {
		this.fromUser = fromUser;
		// manage both sides of the relationship
		fromUser.addSentMessage(this);
	}

	/**
	 * Attach the to and from user to this PM. Both sides of the
	 * User/PrivateMessage relationship are managed by this method.
	 * 
	 * It is assumed that the client will do something like this:
	 * 
	 * <code>
	 * <pre>
	 * // User toUser - assume this exists
	 * // User fromUser - assume this exists
	 * PrivateMessage pm = new PrivateMessage();
	 * 
	 * pm.setMessage(theMessage); // assume theMessage exists
	 * pm.setSubject(theSubject); // assume theSubject exists
	 * 
	 * pm.setToFromUser(toUser, fromUser);
	 * </pre>
	 * </code>
	 * 
	 * @param toUser
	 * @param fromUser
	 */
	public void setToFromUser(User toUser, User fromUser) {
		this.setToUser(toUser);
		this.setFromUser(fromUser);
	}
	
	public String getNumber() {
        return number;
    }
    public void setNumber(String number) {
        this.number = number;
    }
	
	@Override
    public String toString() {
        return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)
        .append("version", getVersion())
        .append("message", message)
        .append("dateRead", dateRead)
        .append("dateCreated", getDateCreated())
        .append("toMember", toUser)
        .append("fromMember", fromUser)
        .append("number", number)
        .append("subject", subject).toString();
    }
    
    // TODO add equals and hashcode
}

